#19.删除链表的倒数第N个节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from typing import Optional
from LeetCode.Hot24 import ListNode

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy_dead=ListNode(0,head)

        slow=fast=dummy_dead

        for i in range(n+1):
            fast=fast.next

        while fast!=None:
            slow=slow.next
            fast=fast.next

        slow.next=slow.next.next
        return dummy_dead.next